package com.example.order.controller;

import com.example.order.client.ProductClient;
import com.example.order.dataObject.ProductInfoOutput;
import com.example.order.dto.CartDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

@RestController
public class ClientController {
    @Autowired
    private RestTemplate  restTemplate;

    @Autowired
    private ProductClient productClient;

    @Autowired
    private LoadBalancerClient  loadBalancerClient;

    @RequestMapping("/getProductMsg")
    public String getProductMsg(){

        //1
//        RestTemplate restTemplate = new RestTemplate();
//        String response = restTemplate.getForObject("http://localhost:39201/msg", String.class);
//        Console.log("response=================="+response);

//        2
//        ServiceInstance instance = loadBalancerClient.choose("PRODUCT");
//        String url = String.format("http://%s:%s", instance.getHost(), instance.getPort()) + "/msg";
//        String response = restTemplate.getForObject(url, String.class);

       //3
//        String response = restTemplate.getForObject("http://PRODUCT/msg", String.class);

        return productClient.proDuctMag();
    }

    @RequestMapping("/getProductList")
    public String getProductList(){
        List<ProductInfoOutput> list = productClient.listForOrder(Arrays.asList("157875196366160022", "157875227953464068"));
        return list.toString();
    }


    @RequestMapping(value = "/decreaseStock",method = RequestMethod.GET)
    public String decreaseStock(){
        CartDTO d= new CartDTO("157875196366160022",3);
         productClient.decreaseStock(Arrays.asList(d));
        return null;
    }




}
